package chixing.day19.threadlocal;

public class Test {
    private ThreadLocal<Integer> balance = ThreadLocal.withInitial(()->0);//初始化一个线程变量，并给一个初始值为0

    public void deposit(int amount){
        balance.set(balance.get()+amount);
        System.out.println(Thread.currentThread().getName() + "存入后的余额：" + balance.get());
    }

    public static void main(String[] args) {
        Test test = new Test();
        Runnable task = () -> {
            for (int i = 0; i < 3; i++) {
                test.deposit(100);
            }
        };
        new Thread(task,"线程A").start();
        new Thread(task,"线程B").start();

    }

}
